﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.ViewQuery;

public partial class Theme_DL_AllCourseList :PowerCreator.LMS.Web.Base.WebBasePage
{
   protected  Course opCourse = new Course();
    Catalog opCatalog = new Catalog();
    ViewQuery opViewQuery = new ViewQuery();
    CatalogCourse opCatalogCourse = new CatalogCourse();
    PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
   

    private string _SQL_Catalog_Course = "select "
        + " tcc.CatalogcouID,tg.CatalogId,tg.CatalogName,tc.Course_Id,tc.CourseName,tc.CourseMostlyTeacherName,tc.CourseIsCompulsory,tc.CourseState ,tc.CourseResourceNum"
        + " from TCatalogCourse tcc left join TCatalog tg on tcc.FKCatalogcouCatalogID=tg.CatalogId "
        + " left join TO_Course tc on tcc.FKCatalogcouCourseID=tc.Course_Id ";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "课程菜单";
        if (!IsPostBack)
        {
            //btnDel.Visible = IsLicensed("DelCourse");
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderList"] = "";//排序
            ViewState["OrderType"] = " desc ";//排序方式
            InitPage();
            BinderCatalog();
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    protected override void InitPage()
    {
        base.InitPage();
    }
   

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(_SQL_Catalog_Course + " where 1=1 and tc.Used=1");

        if (ddl_Catalog.SelectedValue!="0")
        {
            sbSearch.Append(string.Format(" and tg.CatalogId={0} ", ddl_Catalog.SelectedValue));
        }

        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
        {
            sbSearch.Append(string.Format(" and tc.CourseName like '%{0}%'", txt_KeyWord.Text));
        }
        return sbSearch.ToString();

    }
    protected void btn_seach_Click(object sender, EventArgs e)
    {
        //ViewState["DomainID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    private void BinderCatalog()
    {
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        //opCatalog.GetCatalogTreeView(dt,ref tv_Catalog);

        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_Catalog.Items.Insert(0, li);
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opViewQuery.Count(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderList = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderList))
            search += " order by " + orderList + ViewState["OrderType"].ToString();
        DataTable dt = opViewQuery.Query(search,LMSPageSize,pageIndex);
        dt.Columns.Add("bg");
        int iResult = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            int k = Math.DivRem(i, 2, out iResult);
            if (iResult == 0)
                dt.Rows[i]["bg"] = "#FFFFFF";
            if (iResult == 1)
                dt.Rows[i]["bg"] = "#EEEEEE";
        }
        rptCourse.DataSource = dt;
        rptCourse.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }  

    protected void btn_AddFKCourse_Click(object sender, EventArgs e)
    {
        string url = "CourseSelect.aspx?CatalogID=" + ddl_Catalog.SelectedValue.ToString();
        Script.RedirectURL(url);
    }
    protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "EditCourseInfo.aspx?CourseID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }

    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}
